/**
 * Created by sxx on 2017/5/3.
 */
import "./index.less";

import userListTpl from "./user.handlebars";

const TypeRefer = {
    '01': '管理员',
    '02': '普通用户'
};
const token = sessionStorage.getItem('supplier_token');

var PublicBiddingList = function() {

    return {
        //main function to initiate the module
        init: function() {
            this.getData();
        },

        // 初始化表格
        tabelInit: function() {
            if (!jQuery().dataTable) {
                return;
            }

            // begin first table
            $('#sample_1').dataTable({
                "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false },
                    null
                ],

                "aLengthMenu": [
                    [5, 15, 20, -1],
                    [5, 15, 20, "All"] // change per page values here
                ],
                // set the initial value
                "iDisplayLength": 10,
                "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
                "sPaginationType": "bootstrap",
                "oLanguage": {
                    "sLengthMenu": "_MENU_ records per page",
                    "oPaginate": {
                        "sPrevious": "Prev",
                        "sNext": "Next"
                    }
                },
                "aoColumnDefs": [{
                    'bSortable': false,
                    'aTargets': [0]
                }]
            });

            jQuery('#sample_1 .group-checkable').change(function() {
                var set = jQuery(this).attr("data-set");
                var checked = jQuery(this).is(":checked");
                jQuery(set).each(function() {
                    if (checked) {
                        $(this).attr("checked", true);
                    } else {
                        $(this).attr("checked", false);
                    }
                });
                jQuery.uniform.update(set);
            });

            jQuery('#sample_1_wrapper .dataTables_filter input').addClass("m-wrap medium"); // modify table search input
            jQuery('#sample_1_wrapper .dataTables_length select').addClass("m-wrap small"); // modify table per page dropdown
            //jQuery('#sample_1_wrapper .dataTables_length select').select2(); // initialzie select2 dropdown


        },

        // 获取数据
        getData: function() {
            let self = this;
            $.ajax({
                url: HOST_ADDR + '/cpmp/splrSlfMng/acnt',
                type: 'get',
                dataType: 'json',
                contentType: "application/json",
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                success: function(resp) {
                    self.render(resp);
                    // // //页面跳转到index
                    // alert('新增用户成功');
                    // location.href = '/supplier_self/user_manage.html';

                },
                error: function(err) {
                    console.log(err);
                }

            });
        },

        // 渲染数据
        render: function(resp) {
            $.each(resp.data,function(index,item){
                item.indx = index + 1;
                item.type = TypeRefer[item.acntTyp];
                item.operate = (item.acntTyp=="02") ? 1 : 0;
            });

            var test1 = userListTpl({ userList: resp.data });

            $('.J_user-list').html(test1);
            this.bindEvent();
        },

        // 绑定事件
        bindEvent: function() {
            let self = this;
            $('.J_delete').click(function() {
                var id = $(this).parents('.J_user-item').data('id');
                $(function() {
                    $('#myModal').modal('show');
                    $('.J_ensure_delete').unbind().click(function() {
                        $('#myModal').modal('hide');
                        self.deleteUser(id);
                    });

                });
            })
        },

        // 删除用户
        deleteUser: function(id) {
            let self = this;
            $.ajax({
                url: HOST_ADDR + '/cpmp/splrSlfMng/delAcnt/' + id,
                type: 'post',
                dataType: 'json',
                contentType: "application/json",
                headers: {
                    'access_token': token,
                    'login_type': 'splr'
                },
                success: function(resp) {
                    console.log(resp);
                    if(resp.success && resp.msgCod==='S0002'){
                        alert('删除用户成功');
                        self.init();
                    }
                    else{
                        alert('删除用户失败，'+resp.msgCod+' '+resp.msgInfo);
                    }
                },
                error: function(err) {
                    console.log(err);
                }

            });
        }
    };

}();

jQuery(document).ready(function() {
    App.init();
    PublicBiddingList.init();

});